package com.korion.aidldemo.im;

import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;

/**
 * @author chenyinying
 * @date 2020-02-13
 */
public class Client extends IClient.Stub {

    private IBinder server;
    private IMessageListener mListener;
    private Handler mHandler;

    public Client(IBinder server, Handler handler){
        this.server = server;
        mHandler = handler;
    }

    @Override
    public void sendMessage(final Message message) throws RemoteException {
        if (mListener != null){
            try {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mListener.onMessageReceivce(message);
                    }
                });
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public void  setMessageListener(IMessageListener listener){
        mListener = listener;
    }

    public IServer getServer(){
        return Server.asInterface(server);
    }
}
